#include "tablehandler.h"

TableHandler::TableHandler()
{
}

TableHandler::TableHandler(char* name)
{
    mView.setWindowTitle(name);
}

//Construct a table handler and initialize it with a fileParser
TableHandler::TableHandler(FileParser& fileParser, char* name )
{
    //Set the name of the window
    mView.setWindowTitle(name);
    //Set the headers and items for the model
    this->setHeaders(*fileParser.getHeaders());
    this->setModelItemRows(*fileParser.getListItems());
    mView.setModel(&mModel);

}

//Sets the headers for this model
void TableHandler::setHeaders(QStringList& headers)
{
    mModel.setHorizontalHeaderLabels(headers);
}

//Fills this model item with a list of QStringLists
void TableHandler::setModelItemRows(QList<QStringList*>& list)
{
    //Iterate through each item on the QStringList and
    QList<QStringList*>::iterator itemIter;
    for (itemIter=list.begin(); itemIter !=list.end(); itemIter++)
    {
        QStringList::iterator stringIter;
		//This Qlist represents a row on the model
        QList<QStandardItem*> *standardList = new QList<QStandardItem*>();
		//Append each string in the string list to the standardList
        for (stringIter=(*itemIter)->begin(); stringIter != (*itemIter)->end(); stringIter++)
        {
            standardList->append(new QStandardItem(*stringIter));
        }

        mModel.appendRow(*standardList);
    }
}

//Return a reference to the view
QTableView* TableHandler::getViewPointer()
{
	return &mView;
}
